package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.entity.SmsCouponCategory;
import com.mall.service.SmsCouponCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "SmsCouponCategoryController", description = "优惠券与商品种类管理")
@RestController
@RequestMapping("/couponcategories")
public class SmsCouponCategoryController {
    @Resource
    private SmsCouponCategoryService smsCouponCategoryService;

    @ApiOperation(value = "新增优惠券与商品种类关系")
    @PostMapping
    public Result save(@RequestBody SmsCouponCategory smsCouponCategory) {
        boolean flag = smsCouponCategoryService.save(smsCouponCategory);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @ApiOperation(value = "根据id删除优惠券与商品种类关系")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsCouponCategoryService.deleteById(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据id查询优惠券与商品种类关系")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsCouponCategory smsCouponCategory = new SmsCouponCategory();
        smsCouponCategory = smsCouponCategoryService.getById(id);
        Integer code = smsCouponCategory !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsCouponCategory !=null ? "successful":"failed,please try again";
        return new Result(smsCouponCategory,code,msg);
    }

    @ApiOperation(value = "查询全部优惠券与商品种类关系")
    @GetMapping
    public Result getAll(){
        List<SmsCouponCategory> list = smsCouponCategoryService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsCouponCategoryService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsCouponCategory> getPage(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam String title) {
        IPage<SmsCouponCategory> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsCouponCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        return smsCouponCategoryService.page(page,queryWrapper);
    }
}
